package com.party.admin.web.controller.mall;

import com.party.admin.biz.mall.MallShowCategoryBizService;
import com.party.admin.utils.RealmUtils;
import com.party.admin.web.dto.AjaxResult;
import com.party.common.paging.Page;
import com.party.core.model.BaseModel;
import com.party.core.model.mall.MallShowCategory;
import com.party.core.model.mall.MallShowCategoryQuery;
import com.party.core.model.mall.MallShowGoods;
import com.party.core.model.member.MemberGroup;
import com.party.core.service.mall.IMallShowCategoryService;
import com.party.core.service.mall.IMallShowGoodsService;
import com.party.core.service.mall.biz.GoodsShowCountBizService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 导购商品类型
 * @author cyl
 * @date 2017-8-25
 * @time 11:51
 */
@Controller
@RequestMapping("/mall/showcategory")
public class MallShowCategoryController {
	
	protected Logger logger = LoggerFactory.getLogger(MallShowCategoryController.class);


	@Autowired
	private IMallShowCategoryService mallShowCategoryService;
	
	@Autowired
	private MallShowCategoryBizService mallShowCategoryBizService;

	@Autowired
	private GoodsShowCountBizService goodsShowCountBizService;

	@Autowired
	private IMallShowGoodsService mallShowGoodsService;
	
	@RequestMapping("list")
	public ModelAndView categoryList(MallShowCategoryQuery mallShowCategoryQuery,Page page) {
		ModelAndView mv = new ModelAndView("mall/showCategoryList");
		mv.addObject("subjectId", mallShowCategoryQuery.getSubjectId());
		mv.addObject("page", page);
		List<MallShowCategory> list = mallShowCategoryBizService.list(mallShowCategoryQuery, page);
		mv.addObject("categoryList", list);
		return mv;
	}
	
	@RequestMapping("categoryform")
	public ModelAndView categoryForm(String id,String subjectId) {
		ModelAndView mv =new ModelAndView("mall/showCategoryForm");
		mv.addObject("subjectId", subjectId);
		if(StringUtils.isNotEmpty(id)) {
			MallShowCategory category = mallShowCategoryService.get(id);
			mv.addObject("category", category);
		}
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("save")
	public AjaxResult saveCategory(MallShowCategory category) {
		MemberGroup member = RealmUtils.getCurrentUser();
		category.setUpdateBy(member.getId());
		if(StringUtils.isEmpty(category.getId())) {
			category.setCreateBy(member.getId());
			mallShowCategoryService.insert(category);
		}else {
			mallShowCategoryService.update(category);
		}
		return new AjaxResult(true);
	}
	
	@ResponseBody
	@RequestMapping("delete")
	public AjaxResult delCategory(String id) {
		if(StringUtils.isEmpty(id)) {
			return new AjaxResult(false);
		}else {
			// 删除前先判断是否有商品
			MallShowGoods param = new MallShowGoods();
			param.setCategoryId(id);
			param.setDelFlag(BaseModel.DEL_FLAG_NORMAL);
			List<MallShowGoods> goodsList = mallShowGoodsService.listPage(param, new Page(1, 1));
			if (CollectionUtils.isNotEmpty(goodsList)) {
				return AjaxResult.error("该类型下已经有商品, 不能删除");
			}

			// 统计商品量
			try {
				goodsShowCountBizService.delGoodsNum(id);
			} catch (Exception e) {
				logger.error("删除主题商品类型时, 统计商品量异常", e);
			}

			mallShowCategoryService.delete(id);
			return new AjaxResult(true);
		}
	}

}
